package com.smile.frame.config.log.systemlog;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * systemLog 系统日志处理切面
 *
 * @author Xy.wang
 * @date 2021/7/22 11:42

 */
@Aspect
@Slf4j
@Order(1)
@Component
public class LogAspect {

    @Around(value = "@annotation(systemLog)")
    public Object doAroundMethod(ProceedingJoinPoint pjd, SystemLog systemLog) throws Throwable {
        //方法名称
        String methName = pjd.getSignature().toShortString();
        // 开始时间
        Date startTime = new Date();
        // 执行方法
        Object respDto = null;
        //异常
        Exception exception = null;
        try {
            respDto = pjd.proceed();
        } catch (Exception e) {
            exception = e;
        }
        // 结束时间
        Date endTime = new Date();
        //获取日志处理
        AbstractHandlerLogger logger = HandleLoggerFactory.initLoggerHandler(methName,
                respDto,
                systemLog,
                startTime,
                endTime,
                pjd.getArgs(),
                exception);
        //输出日志
        logger.outPutLogger();
        //获取返回类
        return respDto;
    }

}
